package org.nlp2rdf.navigator.client;

import org.nlp2rdf.navigator.client.common.UserDTO;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;

public class LoginWidget extends Composite {

	private Application app;
	private Button loginButton = new Button("Login");;
	private Button logOutButton = new Button("Logout");
	private TextBox loginField = new TextBox();

	final String enterUsername = "enter username...";

	WorkerServiceAsync worker = WorkerServiceAsync.Util.getInstance();

	public LoginWidget(Application application) {
		this.app = application;
		HorizontalPanel loginPanel = new HorizontalPanel();

//		Label title = new Label("Login");
//		loginPanel.add(title);

		
		
		
		initLoginButton();
		initLoginField();
		initLogoutButton();

		//DEACTIVATE FOR NOW
//		loginPanel.add(loginField);
//		loginPanel.add(loginButton);
//		loginPanel.add(logOutButton);

		// mainPanel.add(loginPanel);
		// mainPanel.addStyleName("module-border");
		// mainPanel.setWidth("95%");
		// mainPanel.addStyleName("greyed");
//		draw();
		initWidget(loginPanel);
	}
	
	public void initLoginField(){
		loginField.setText(enterUsername);

		loginField.addFocusHandler(new FocusHandler() {

			public void onFocus(FocusEvent event) {
				if (loginField.getText().equalsIgnoreCase(enterUsername)) {
					loginField.setText("");
				}

			}
		});
		loginField.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				if (loginField.getText().isEmpty()) {
					loginField.setText(enterUsername);
				}
				
			}
		});
		
	}
	
	private void initLogoutButton() {
		logOutButton.setVisible(false);
		logOutButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent arg0) {
				logOutButton.setVisible(false);
				loginButton.setVisible(true);
				loginField.setText(enterUsername);
				loginField.setEnabled(true);

			}
		});
	}

	private void initLoginButton() {
		loginButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent arg0) {
				if (loginButton.getText().startsWith("enter")) {
					loginField.setText("enter username HERE...");
				} else {
					worker.login(loginField.getText(), "", new AsyncCallback<UserDTO>() {

						public void onFailure(Throwable arg0) {
							app.reportError(arg0);
						}

						public void onSuccess(UserDTO user) {
							app.setUser(user);
							loginButton.setVisible(false);
							logOutButton.setVisible(true);
							loginField.setEnabled(false);

						}
					});

				}

			}
		});

	}
//
//	private void draw() {
//
//	}

}